package com.jh.complicate.rpc.v2.client;

import com.jh.complicate.rpc.v2.api.RPCRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author Administrator
 * @projectName newforward
 * @title
 * @package com.jh.complicate.rpc.v1.client
 * @description
 * @date 2019/6/12 00:04
 */
public class RemoteInvocationHandler implements InvocationHandler {

    private String host;
    private int port;

    public RemoteInvocationHandler(String host, int port) {
        this.host = host;
        this.port = port;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Come in");

        // 组装远程调度信息
        RPCRequest request = new RPCRequest();
        request.setClassName(method.getDeclaringClass().getName());
        request.setMethod(method.getName());
        // 设置版本号，调用哪个版本的接口
        request.setVersion("v2.0");
        request.setParameters(args);

        // 进行远程调度
        RPCNetTransport netTransport = new RPCNetTransport(host, port);
        // 返回调用结果
        Object result = netTransport.send(request);
        return result;
    }
}
